// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.example.cloudwatch;

// snippet-start:[cloudwatch.java2.put_metric_alarm.main]
// snippet-start:[cloudwatch.java2.put_metric_alarm.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;
import software.amazon.awssdk.services.cloudwatch.model.Dimension;
import software.amazon.awssdk.services.cloudwatch.model.PutMetricAlarmRequest;
import software.amazon.awssdk.services.cloudwatch.model.ComparisonOperator;
import software.amazon.awssdk.services.cloudwatch.model.Statistic;
import software.amazon.awssdk.services.cloudwatch.model.StandardUnit;
import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException;
// snippet-end:[cloudwatch.java2.put_metric_alarm.import]

/**
 * Before running this Java V2 code example, set up your development
 * environment, including your credentials.
 *
 * For more information, see the following documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class PutMetricAlarm {
    public static void main(String[] args) {

        final String usage = """

                Usage:
                  <alarmName> <instanceId>\s

                Where:
                  alarmName - An alarm name to use.
                  instanceId - An instance Id value .
                """;

        if (args.length != 2) {
            System.out.println(usage);
            System.exit(1);
        }

        String alarmName = args[0];
        String instanceId = args[1];
        CloudWatchClient cw = CloudWatchClient.builder()
                .region(Region.US_EAST_1)
                .build();

        putMetricAlarm(cw, alarmName, instanceId);
        cw.close();
    }

    public static void putMetricAlarm(CloudWatchClient cw, String alarmName, String instanceId) {
        try {
            Dimension dimension = Dimension.builder()
                    .name("InstanceId")
                    .value(instanceId).build();

            PutMetricAlarmRequest request = PutMetricAlarmRequest.builder()
                    .alarmName(alarmName)
                    .comparisonOperator(ComparisonOperator.GREATER_THAN_THRESHOLD)
                    .evaluationPeriods(1)
                    .metricName("CPUUtilization")
                    .namespace("AWS/EC2")
                    .period(60)
                    .statistic(Statistic.AVERAGE)
                    .threshold(70.0)
                    .actionsEnabled(false)
                    .alarmDescription("Alarm when server CPU utilization exceeds 70%")
                    .unit(StandardUnit.SECONDS)
                    .dimensions(dimension)
                    .build();

            cw.putMetricAlarm(request);
            System.out.printf("Successfully created alarm with name %s", alarmName);

        } catch (CloudWatchException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
}
// snippet-end:[cloudwatch.java2.put_metric_alarm.main]
